package logic

import (
	"context"
	"fmt"
	"weixin/models/mysql"

	"weixin/weixinrpc/internal/svc"
	"weixin/weixinrpc/weixinrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserRegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserRegisterLogic {
	return &UserRegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserRegisterLogic) UserRegister(in *weixinrpc.UserRegisterRequest) (*weixinrpc.UserRegisterResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	mysql.DB.Debug().Where("nickname = ?", in.Nickname).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("手机号注册,请重新注册")
	}
	m := mysql.User{
		Nickname:  user.Nickname,
		Name:      user.Name,
		HeadImage: user.HeadImage,
		Tel:       user.Tel,
		Uptime:    user.Uptime,
		Price:     user.Price,
	}
	mysql.DB.Debug().Create(&m)
	return &weixinrpc.UserRegisterResponse{
		Id: int64(m.ID),
	}, nil
}
